package com.example.foodisimportant.Canteens;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

import com.example.foodisimportant.Adapter.ViewAdapter;
import com.example.foodisimportant.Bean.PostEntity;
import com.example.foodisimportant.Body.DishList;
import com.example.foodisimportant.Body.ResponseBody;
import com.example.foodisimportant.Body.dishBody;
import com.example.foodisimportant.Network.NetworkSettings;
import com.example.foodisimportant.Network.SearchNetworkThread;
import com.example.foodisimportant.R;
import com.example.foodisimportant.StudentViewActivity.Fragment.HomeFragment;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.json.JSONArray;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.FutureTask;

public class SearchInformationActivity extends AppCompatActivity {

    private ImageButton mIbbacktohome;
    private RecyclerView mRvresult;
    private ViewAdapter SearchinformationAdapter;
    private String keyword;
    private ArrayList<PostEntity> resultList=new ArrayList<PostEntity>();
    private final ObjectMapper mapper = new ObjectMapper();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_information);
        init();
        initRecyclerView();
        mIbbacktohome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(SearchInformationActivity.this, HomeFragment.class);
                startActivity(intent);
            }
        });

    }

    private void init(){
        mIbbacktohome=findViewById(R.id.backtohome);
        mRvresult=findViewById(R.id.searchinformation);
        Intent intent=getIntent();
        keyword=intent.getStringExtra("keyword");
        new Thread(new Runnable() {
            @Override
            public void run() {
                    FutureTask<String> searchTask = new FutureTask<>(new SearchNetworkThread(keyword, NetworkSettings.GET_DISH));
                    Thread thread =new Thread(searchTask);
                    thread.start();
                    try{
                        ResponseBody body = mapper.readValue(searchTask.get(), ResponseBody.class);
                       JSONArray array=new JSONArray(body.getData());
                        for (int i=0;i<array.length();i++){
                            JSONObject object=array.getJSONObject(i);
                            String dishName=object.getString("dishName");
                            String dishFlavour=object.getString("dishFlavour");
                            String dishstrImg=object.getString("strImage");
                            String dishPrice=object.getString("dishPrice1");
                            PostEntity postEntity=new PostEntity();
                            postEntity.setName(dishName);
                            postEntity.setPrice(dishPrice);
                            postEntity.setFlavour(dishFlavour);
                            postEntity.setNumber("TOP"+i);
                            postEntity.setPicture(dishstrImg);
                            resultList.add(postEntity);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        }).start();
    }

    /**
     * 对recycleview进行设置
     */
    private void initRecyclerView(){
        //创建adapter
        SearchinformationAdapter = new ViewAdapter(SearchInformationActivity.this, resultList);
        //给RecyclerView设置adapter
        mRvresult.setAdapter(SearchinformationAdapter);
        //设置layoutManager,可以设置显示效果，是线性布局、grid布局，还是瀑布流布局
        //参数是：上下文、列表方向（横向还是纵向）、是否倒叙
        mRvresult.setLayoutManager(new LinearLayoutManager(SearchInformationActivity.this,LinearLayoutManager.VERTICAL,false));
        //设置item的分割线
        mRvresult.addItemDecoration(new DividerItemDecoration(SearchInformationActivity.this,DividerItemDecoration.VERTICAL));
        //RecyclerView中没有item的监听事件，需要自己在适配器中写一个监听事件的接口。参数根据自定义
        SearchinformationAdapter.setOnItemClickListener(new ViewAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, PostEntity data) {
                Intent intent=new Intent(SearchInformationActivity.this,DishInformationActivity.class);
                startActivity(intent);
            }
        });
    }
}